<?php
namespace app\common\search\jy;

/**
 * 提供搜索条件
 */
class OrderRefund extends Base{
	protected $request = 'jy\\OrderRefund';

	// 私有列表查询条件生成
	public function private_list($model) {
		$post = $this->request->getPrivateListSearch();
		$this->search4arr($model, ['type', 'product', 'status'], $post);
		$this->order($model);
		return $model;
	}

	//店铺查询条件生成
	public function store_private_list($model) {
		$post = $this->request->getStorePrivateList();
		$this->store_private_list_default($model, $post);
		$this->search4arr($model, ['sid', 'type', 'product', 'status'], $post);
		$this->order($model);
		return $model;
	}

	//店铺查询默认条件生成
	public function store_private_list_default($model, $post) {
		$model->alias('OR')->join('Order O', 'OR.oid = O.id', 'left');
		$model->field('OR.*');
	}

	//是否获取商品信息
	public function product($model, $post) {
		if(isset($post['product'])) {
			$model->append(['order_refund_product']);
		}
	}

	//sid参数处理方法
	protected function sid($model, $post) {
		if($post['sid']) {
			$model->where('O.sid', $post['sid']);
		}
	}

	//type参数处理方法
	protected function type($model, $post) {
		if($post['type']) {
			$funName = 'type_' . $post['type'];
			if (method_exists($this, $funName)) {
				$this->$funName($model);
			}
		}
		return $model;
	}

	//查询已完成退款
	protected function type_over($model) {
		$statusEnum = loader()->loadEnum('OrderRefundStatus');
		$statusArr = $statusEnum->over();
		$key = $this->get_search_key($model, 'status');
		$model->whereIn($key, $statusArr);
	}

	//查询未完成退款
	protected function type_incomplete($model) {
		$statusEnum = loader()->loadEnum('OrderRefundStatus');
		$statusArr = $statusEnum->over();
		$key = $this->get_search_key($model, 'status');
		$model->whereNotIn($key, $statusArr);
	}

	//status参数处理方法
	protected function status($model, $post) {
		$status = $post['status'];
		if (ctype_digit($status)) {
		    $model->where('OR.status', $status);
		} else {
			$funName = 'status_' . $status;
		    if (method_exists($this, $funName)) {
				$this->$funName($model);
			}
		}
	}

	//待处理订单
	protected function status_pending($model) {
		$arr = loadEnum('OrderRefundStatus')->over();
		$model->whereNotIn('OR.status', $arr);
	}

	//已退款
	protected function status_refunded($model) {
		$arr = loadEnum('OrderRefundStatus')->refunded();
		$model->whereIn('OR.status', $arr);
	}

	//已拒绝
	protected function status_refuse($model) {
		$arr = loadEnum('OrderRefundStatus')->abolish();
		$model->whereIn('OR.status', $arr);
	}

	//已取消
	protected function status_abolish($model) {
		$arr = loadEnum('OrderRefundStatus')->abolish();
		$model->whereIn('OR.status', $arr);
	}

	//已完成
	protected function status_over($model) {
		$arr = loadEnum('OrderRefundStatus')->over();
		$model->whereIn('OR.status', $arr);
	}
}
